package com.itheima.web.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

// 全局异常处理器
@ControllerAdvice
public class GlobalExceptionAdvice {

    // 捕获预期异常
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity handlerBusiness(BusinessException bex) {
        // 打印异常
        bex.printStackTrace();

        // 返回错误提示
        Map<String, String> errorResult = new HashMap<>();
        errorResult.put("message", bex.getMessage());
        return ResponseEntity.status(500).body(errorResult);
    }

    // 捕获非预期异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity handlerException(Exception ex) {
        // 打印异常
        ex.printStackTrace();

        // 返回错误提示
        Map<String, String> errorResult = new HashMap<>();
        errorResult.put("message", "系统繁忙，请稍后重试...");
        return ResponseEntity.status(500).body(errorResult);
    }
}
